package cn.cdeden.aiqa.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cdeden.common.idempotent.annotation.RepeatSubmit;
import cn.cdeden.common.log.annotation.Log;
import cn.cdeden.common.web.core.BaseController;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.validate.AddGroup;
import cn.cdeden.common.core.validate.EditGroup;
import cn.cdeden.common.log.enums.BusinessType;
import cn.cdeden.common.excel.utils.ExcelUtil;
import cn.cdeden.aiqa.domain.vo.WikiSensitiveVo;
import cn.cdeden.aiqa.domain.bo.WikiSensitiveBo;
import cn.cdeden.aiqa.service.IWikiSensitiveService;
import cn.cdeden.common.core.utils.page.TableDataInfo;

/**
 * 敏感词管理
 * 前端访问路由地址为:/aiqa/sensitive
 *
 * @author Nick
 * @date 2024-12-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sensitive")
public class WikiSensitiveController extends BaseController {

    private final IWikiSensitiveService wikiSensitiveService;

    /**
     * 查询敏感词管理列表
     */
    @SaCheckPermission("aiqa:sensitive:list")
    @GetMapping("/list")
    public TableDataInfo<WikiSensitiveVo> list(WikiSensitiveBo bo, PageQuery pageQuery) {
        return wikiSensitiveService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出敏感词管理列表
     */
    @SaCheckPermission("aiqa:sensitive:export")
    @Log(title = "敏感词管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WikiSensitiveBo bo, HttpServletResponse response) {
        List<WikiSensitiveVo> list = wikiSensitiveService.queryList(bo);
        ExcelUtil.exportExcel(list, "敏感词管理", WikiSensitiveVo.class, response);
    }

    /**
     * 获取敏感词管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("aiqa:sensitive:query")
    @GetMapping("/{id}")
    public R<WikiSensitiveVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wikiSensitiveService.queryById(id));
    }

    /**
     * 新增敏感词管理
     */
    @SaCheckPermission("aiqa:sensitive:add")
    @Log(title = "敏感词管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WikiSensitiveBo bo) {
        return toAjax(wikiSensitiveService.insertByBo(bo));
    }

    /**
     * 修改敏感词管理
     */
    @SaCheckPermission("aiqa:sensitive:edit")
    @Log(title = "敏感词管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WikiSensitiveBo bo) {
        return toAjax(wikiSensitiveService.updateByBo(bo));
    }

    /**
     * 删除敏感词管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("aiqa:sensitive:remove")
    @Log(title = "敏感词管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wikiSensitiveService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 敏感字过滤
     */
    @PostMapping("/check-sensitive")
    public R<Void> checkSensitive(@RequestBody String text) {
        if (wikiSensitiveService.isSensitive(text)) {
            return R.fail(500, "存在敏感字");
        } else {
            return R.ok();
        }
    }
}
